package com.springboot5.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot5.demo.bean.User;
import com.springboot5.demo.serrvice.UserService;
import com.springboot5.demo.serrvice.impl.USerServiceImple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collections;
import java.util.List;

@Controller
public class Logincontroller {

    @GetMapping("/")
    public String login(){
        return "login";
    }

    //@Autowired
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("/main")
    public String main(Model model){
       /* ValueOperations<String,String > operations= stringRedisTemplate.opsForValue();
        String main= operations.get("/main");
        String sql=operations.get("/sql");
        model.addAttribute("mainscan",main);
        model.addAttribute("sqlscan",sql);*/
        return "main";
    }

    @Autowired
    UserService userService;

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")int pn, Model model){
        //List<User> list =userService.list();
       Page<User> userPage=new Page<User>(pn,2);
       Page<User> page= userService.page(userPage,null);
       //查询数据库列表
       /*
         * 查询数据列表
         * protected List<T> records = Collections.emptyList();
         */

       List<User> records=page.getRecords();
       model.addAttribute("userpage",page);
        return "dynamic_table";
    }

    @GetMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id,
                             @RequestParam(value = "pn" ,defaultValue = "1") int pn,
                             RedirectAttributes redirectAttributes){
        redirectAttributes.addAttribute("pn",pn);
        userService.removeById(id);
        return "redirect:/dynamic_table";
    }
}
